import Vue from "vue";
import Router from "vue-router";
import localCache from "@/utils/cache";

Vue.use(Router);

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/login.vue"),
    },
    {
      path: "/",
      name: "main",
      component: () => import("@/views/main/main.vue"),
      children: [
        {
          // 营销状况
          path: "",
          name:'main/getCountData',
          component: () => import("@/views/main/system/role/role.vue"),
          // component: () => import("@/views/main/system/role/role1.vue"),
        },
        {
          //机构管理
          name: "account/index",
          path:'account/index',
          component: () => import("@/views/main/system/menu/menu.vue"),
        },
        { 
          // 权限管理
          path: "permission",
          name: "permission",
          component: () => import("@/views/main/system/permission/permission.vue"),
        },
        // 扩展管理
        {
          path: "expend",
          name: "expend",
          component: () => import("/src/views/main/system/expend/expend.vue"),
        },
      ],
    },
  ],
});

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    const token = localCache.getCache("token");
    if (token) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
